package com.java110.mall.shop.api;

import com.alibaba.fastjson.JSONObject;
import com.java110.dto.store.StoreInfoDto;
import com.java110.po.store.StoreInfoPo;
import com.java110.mall.shop.bmo.storeInfo.IDeleteStoreInfoBMO;
import com.java110.mall.shop.bmo.storeInfo.IGetStoreInfoBMO;
import com.java110.mall.shop.bmo.storeInfo.ISaveStoreInfoBMO;
import com.java110.mall.shop.bmo.storeInfo.IUpdateStoreInfoBMO;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping(value = "/storeInfo")
public class StoreInfoApi {

    @Autowired
    private ISaveStoreInfoBMO saveStoreInfoBMOImpl;
    @Autowired
    private IUpdateStoreInfoBMO updateStoreInfoBMOImpl;
    @Autowired
    private IDeleteStoreInfoBMO deleteStoreInfoBMOImpl;

    @Autowired
    private IGetStoreInfoBMO getStoreInfoBMOImpl;

    /**
     * 微信保存消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /storeInfo/saveStoreInfo
     * @path /app/storeInfo/saveStoreInfo
     */
    @RequestMapping(value = "/saveStoreInfo", method = RequestMethod.POST)
    public ResponseEntity<String> saveStoreInfo(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "name", "请求报文中未包含name");
        Assert.hasKeyAndValue(reqJson, "seq", "请求报文中未包含seq");


        StoreInfoPo storeInfoPo = BeanConvertUtil.covertBean(reqJson, StoreInfoPo.class);
        return saveStoreInfoBMOImpl.save(storeInfoPo);
    }

    /**
     * 微信修改消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /storeInfo/updateStoreInfo
     * @path /app/storeInfo/updateStoreInfo
     */
    @RequestMapping(value = "/updateStoreInfo", method = RequestMethod.POST)
    public ResponseEntity<String> updateStoreInfo(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "name", "请求报文中未包含name");
        Assert.hasKeyAndValue(reqJson, "seq", "请求报文中未包含seq");
        Assert.hasKeyAndValue(reqJson, "storeInfoId", "storeInfoId不能为空");


        StoreInfoPo storeInfoPo = BeanConvertUtil.covertBean(reqJson, StoreInfoPo.class);
        return updateStoreInfoBMOImpl.update(storeInfoPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /storeInfo/deleteStoreInfo
     * @path /app/storeInfo/deleteStoreInfo
     */
    @RequestMapping(value = "/deleteStoreInfo", method = RequestMethod.POST)
    public ResponseEntity<String> deleteStoreInfo(@RequestBody JSONObject reqJson) {
//      Assert.hasKeyAndValue(reqJson, "communityId", "小区ID不能为空");

        Assert.hasKeyAndValue(reqJson, "storeInfoId", "storeInfoId不能为空");


        StoreInfoPo storeInfoPo = BeanConvertUtil.covertBean(reqJson, StoreInfoPo.class);
        return deleteStoreInfoBMOImpl.delete(storeInfoPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param storeId 小区ID
     * @return
     * @serviceCode /storeInfo/queryStoreInfo
     * @path /app/storeInfo/queryStoreInfo
     */
    @RequestMapping(value = "/queryStoreInfo", method = RequestMethod.GET)
    public ResponseEntity<String> queryStoreInfo(@RequestHeader(value = "store-id") String storeId,
                                                 @RequestParam(value = "page") int page,
                                                 @RequestParam(value = "name", required = false) String name,
                                                 @RequestParam(value = "convenienceMenusId", required = false) String convenienceMenusId,
                                                 @RequestParam(value = "storeInfoId", required = false) String storeInfoId,
                                                 @RequestParam(value = "isShow", required = false) String isShow,
                                                 @RequestParam(value = "row") int row) {
        StoreInfoDto storeInfoDto = new StoreInfoDto();
        storeInfoDto.setPage(page);
        storeInfoDto.setRow(row);
        storeInfoDto.setName(name);
        storeInfoDto.setConvenienceMenusId(convenienceMenusId);
        storeInfoDto.setStoreInfoId(storeInfoId);
        storeInfoDto.setIsShow(isShow);
        return getStoreInfoBMOImpl.get(storeInfoDto);
    }

    /**
     * 微信删除消息模板
     *
     * @param storeId 小区ID
     * @return
     * @serviceCode /storeInfo/queryStoreBySeq
     * @path /app/storeInfo/queryStoreBySeq
     */
    @RequestMapping(value = "/queryStoreBySeq", method = RequestMethod.GET)
    public ResponseEntity<String> queryStoreBySeq(@RequestHeader(value = "store-id") String storeId,
                                                  @RequestParam(value = "page") int page,
                                                  @RequestParam(value = "name", required = false) String name,
                                                  @RequestParam(value = "convenienceMenusId", required = false) String convenienceMenusId,
                                                  @RequestParam(value = "storeInfoId", required = false) String storeInfoId,
                                                  @RequestParam(value = "isShow", required = false) String isShow,
                                                  @RequestParam(value = "row") int row) {


        StoreInfoDto storeInfoDto = new StoreInfoDto();
        storeInfoDto.setPage(page);
        storeInfoDto.setRow(row);
        storeInfoDto.setName(name);
        storeInfoDto.setConvenienceMenusId(convenienceMenusId);
        storeInfoDto.setStoreInfoId(storeInfoId);
        storeInfoDto.setIsShow(isShow);
        return getStoreInfoBMOImpl.getStoreBySeq(storeInfoDto);
    }
}
